package com.example.seey2023.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.seey2023.entity.HeSuan;
import com.example.seey2023.entity.NcovData;
import com.example.seey2023.entity.User;
import com.example.seey2023.service.HeSuanService;
import com.example.seey2023.vo.DataView;
import com.example.seey2023.vo.HeSuanVo;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Controller
@RequestMapping("/hesuan")
public class HeSuanController {

    @Autowired
    private HeSuanService heSuanService;

    @RequestMapping("/toHeSuan")
    public String toHeSuan() {
        return "hesuan/hesuan";
    }



    @RequestMapping("/loadAllHeSuan")
    @ResponseBody
    public DataView loadAllHeSuan(HeSuanVo heSuanVo) {
        IPage<HeSuan> page = new Page<>(heSuanVo.getPage(), heSuanVo.getLimit());
        QueryWrapper<HeSuan> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(heSuanVo.getName()),"name",heSuanVo.getName());
        heSuanService.page(page, queryWrapper);
        return new DataView(page.getTotal(), page.getRecords());
    }

    @RequestMapping("/loadOneHeSuan")
    @ResponseBody
    public DataView loadOneHeSuan(HeSuanVo heSuanVo, HttpServletRequest httpServletRequest) {
        IPage<HeSuan> page = new Page<>(heSuanVo.getPage(), heSuanVo.getLimit());
        HttpSession session = httpServletRequest.getSession();
        User user = (User) session.getAttribute("user");
        String username = null;

        if (Objects.nonNull(user) ){
           username = user.getUsername();
        }
        LambdaQueryWrapper<HeSuan> objectLambdaQueryWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<HeSuan> eq = objectLambdaQueryWrapper.eq(HeSuan::getName, username);
        heSuanService.page(page, eq);
        return new DataView(page.getTotal(), page.getRecords());
    }

    @RequestMapping("/addHeSuan")
    @ResponseBody
    public DataView addHeSuan(HeSuan heSuan){
        heSuanService.save(heSuan);
        DataView dataView = new DataView();
        dataView.setCode(200);
        dataView.setMsg("核酸检测记录添加成功！");
        return dataView;
    }

    @RequestMapping("/updateHeSuan")
    @ResponseBody
    public DataView updateHeSuan(HeSuan heSuan){
        heSuanService.updateById(heSuan);
        DataView dataView = new DataView();
        dataView.setCode(200);
        dataView.setMsg("核酸检测记录修改成功！");
        return dataView;
    }

    @RequestMapping("/deleteHeSuan")
    @ResponseBody
    public DataView deleteHeSuan(Integer id){
        heSuanService.removeById(id);
        DataView dataView = new DataView();
        dataView.setCode(200);
        dataView.setMsg("核酸检测记录删除成功！");
        return dataView;
    }

    @RequestMapping("/excelImportHeSuan")
    @ResponseBody
    public DataView excelImportHeSuan(@RequestParam("file") MultipartFile file) throws IOException {
        DataView dataView = new DataView();
        // 1.文件不为空
        if (file.isEmpty()) {
            dataView.setMsg("文件为空，不能上传！");
        }

        // 2.POI获取Excel解析数据
        XSSFWorkbook wb = new XSSFWorkbook(file.getInputStream());
        XSSFSheet sheet = wb.getSheetAt(0);
        // 3.定义一个程序集合 接受 文件中的数据
        List<HeSuan> list = new ArrayList<>();
        XSSFRow row = null;
        // 4.解析数据，装到集合里面
        for (int i = 0; i < sheet.getPhysicalNumberOfRows(); i++) {
            // 4.1 定义实体
            HeSuan heSuan = new HeSuan();
            // 4.2 每一行的数据，放到实体类里面
            row = sheet.getRow(i);
            // 4.3 解析数据
            XSSFCell cell2 = row.getCell(2);
            XSSFCell cell3 = row.getCell(3);
            cell2.setCellType(CellType.STRING);
            cell3.setCellType(CellType.STRING);

            heSuan.setName(row.getCell(0).getStringCellValue());
            heSuan.setAge((int) row.getCell(1).getNumericCellValue());

            heSuan.setPhone(cell2.getStringCellValue());
            heSuan.setCard(cell3.getStringCellValue());
//            heSuan.setPhone(row.getCell(2).getStringCellValue());
//            heSuan.setCard(row.getCell(3).getStringCellValue());

            heSuan.setType(row.getCell(4).getStringCellValue());
            heSuan.setResult(row.getCell(5).getStringCellValue());
            heSuan.setCreateTime(row.getCell(6).getDateCellValue());
            heSuan.setUpdateTime(row.getCell(7).getDateCellValue());
            // 5.添加list集合
            list.add(heSuan);
        }
        // 6. 插入数据库
        heSuanService.saveBatch(list);
        dataView.setCode(200);
        dataView.setMsg("excel插入成功！");
        return dataView;
    }
}
